﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using DoAnWeb.DTO;

namespace DoAnWeb.DAO
{
    public class CT_DonHangDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<CT_DonHangDTO> GetList_DonHang(int _MaDonHang)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaDonHang", _MaDonHang) 
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_DonHang_CT", sqlParameter);
                dp.CloseConnect();
                BindingList<CT_DonHangDTO> List = new BindingList<CT_DonHangDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    CT_DonHangDTO DTO = new CT_DonHangDTO();
                    DTO.MaCTDonHang = int.Parse(row["MaCTDonHang"].ToString());
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString()); 
                    DTO.ThanhTien = int.Parse(row["ThanhTien"].ToString());
                    DTO.TieuDe = row["TieuDe"].ToString();

                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.LoiNhan = row["LoiNhan"].ToString();
                    DTO.HoTen = row["HoTen"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<CT_DonHangDTO> GetList_TinKhuyenMai_ChuaXoa(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaTinKhuyenMai", _Ma) 
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_CT_DonHang_TinKhuyenMai_ChuaGiao", sqlParameter);
                dp.CloseConnect();
                BindingList<CT_DonHangDTO> List = new BindingList<CT_DonHangDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    CT_DonHangDTO DTO = new CT_DonHangDTO();
                    DTO.MaCTDonHang = int.Parse(row["MaCTDonHang"].ToString());
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());

                    DTO.TieuDe = row["TieuDe"].ToString();

                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.LoiNhan = row["LoiNhan"].ToString();
                    DTO.HoTen = row["HoTen"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<CT_DonHangDTO> GetList_DoanhNghiep(string _TenTaiKhoan)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenTaiKhoan", _TenTaiKhoan) 
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_CT_DonHang_DoanhNghiep", sqlParameter);
                dp.CloseConnect();
                BindingList<CT_DonHangDTO> List = new BindingList<CT_DonHangDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    CT_DonHangDTO DTO = new CT_DonHangDTO();
                    DTO.MaCTDonHang = int.Parse(row["MaCTDonHang"].ToString());
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());

                    DTO.TieuDe = row["TieuDe"].ToString();
                    if (int.Parse(row["DaGiao"].ToString()) == 1)
                        DTO.DaGiao = "Đã giao";
                    else
                        DTO.DaGiao = "Chưa giao";

                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.LoiNhan = row["LoiNhan"].ToString();
                    DTO.HoTen = row["HoTen"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }

        public BindingList<CT_DonHangDTO> GetList_TinKhuyenMai_DaXoa(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaTinKhuyenMai", _Ma) 
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetList_CT_DonHang_TinKhuyenMai_DaXoa", sqlParameter);
                dp.CloseConnect();
                BindingList<CT_DonHangDTO> List = new BindingList<CT_DonHangDTO>();
                foreach (DataRow row in dt.Rows)
                {
                    CT_DonHangDTO DTO = new CT_DonHangDTO();
                    DTO.MaCTDonHang = int.Parse(row["MaCTDonHang"].ToString());
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());

                    DTO.TieuDe = row["TieuDe"].ToString();

                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.LoiNhan = row["LoiNhan"].ToString();
                    DTO.HoTen = row["HoTen"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                    List.Add(DTO);
                }
                return List;
            }
            return null;
        }
        
        public bool Add(CT_DonHangDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaDonHang", _DTO.MaDonHang), 
                    new SqlParameter("@MaTinKhuyenMai", _DTO.MaTinKhuyenMai) ,
                    new SqlParameter("@SoLuong", _DTO.SoLuong) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_CT_DonHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public CT_DonHangDTO GetFromId(int _MaCTDonHang)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaCTDonHang", _MaCTDonHang) };
                DataTable dt = dp.SqlExcuteQuery("uproc_GetFromID_CT_DonHang", sqlParameter);
                dp.CloseConnect();
                CT_DonHangDTO DTO = new CT_DonHangDTO();
                foreach (DataRow row in dt.Rows)
                {
                    DTO.MaCTDonHang = int.Parse(row["MaCTDonHang"].ToString());
                    DTO.MaDonHang = int.Parse(row["MaDonHang"].ToString());
                    DTO.SoLuong = int.Parse(row["SoLuong"].ToString());
                    DTO.GiaGoc = int.Parse(row["GiaGoc"].ToString());
                    DTO.GiaBan = int.Parse(row["GiaBan"].ToString());

                    DTO.TieuDe = row["TieuDe"].ToString();

                    DTO.TenTaiKhoan = row["TenTaiKhoan"].ToString();
                    DTO.DiaChi = row["DiaChi"].ToString();
                    DTO.DienThoai = row["DienThoai"].ToString();
                    DTO.LoiNhan = row["LoiNhan"].ToString();
                    DTO.HoTen = row["HoTen"].ToString();
                    DTO.NgayDat = DateTime.Parse(row["NgayDat"].ToString());
                    if (int.Parse(row["DaXoa"].ToString()) == 0)
                        DTO.DaXoa = "Bình thường";
                    else
                        DTO.DaXoa = "Đã Xóa";
                }
                return DTO;
            }
            return null;
        }

        public bool UpdateDaGiao(int _MaCTDonHang)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaCTDonHang", _MaCTDonHang) };
                bool kq = dp.SqlExcuteNonQuery("uproc_UpdateDaGiao_CT_DonHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UpdateChuaGiao(int _MaCTDonHang)
        {
            SqlParameter[] sqlParameter = { new SqlParameter("@MaCTDonHang", _MaCTDonHang) };
            if (dp.OpenConnect())
            {
                bool kq = dp.SqlExcuteNonQuery("uproc_UpdateChuaGiao_CT_DonHang", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

    }
}